 
 import 'package:flutter/material.dart';
 import 'package:osg/controllers/custom_widget.dart'; 
 
//  import 'package:osg/middleware/im_manager.dart';

 import 'package:slideable/slideable.dart';
 import 'package:get/get.dart';

 import 'package:osg/controllers/http.dart';
  // import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart';
  // import 'package:tencent_cloud_chat_sdk/models/v2_tim_user_full_info.dart';

 import '../../controllers/web3.dart';
 import '../web_tabs/home.dart';
 import 'package:intl/intl.dart';


String formatTimestamp(int timestampMillis) {
  // 当前时间（2025-06-14）
  final now = DateTime.now();
  final date = DateTime.fromMillisecondsSinceEpoch(timestampMillis * 1000);
  
  // 计算时间差
  final difference = now.difference(date);
  
  // 1. 今天内：显示小时+分钟
  if (now.day == date.day) {
    return DateFormat('HH:mm').format(date);
  }
  
  // 2. 昨天（24-48小时之间）
  if (difference.inHours >= 24 && difference.inHours < 48) {
    return '昨天 ${DateFormat('HH:mm').format(date)}';
  }
  
  // 3. 前天（48-72小时之间）
  if (difference.inHours >= 48 && difference.inHours < 72) {
    return '前天 ${DateFormat('HH:mm').format(date)}';
  }
  
  // 4. 超过72小时但小于1年
  if (difference.inDays < 365) {
    return DateFormat('MM月dd日 HH:mm').format(date);
  }
  
  // 5. 超过1年
  return DateFormat('yyyy年MM月dd日').format(date);
}


class HomePage extends StatefulWidget {
  HomePage({super.key});

  @override
  State<HomePage> createState() => _WebDetailState();
}


class _WebDetailState extends State<HomePage> {
//  final RxBool dataController;
  final RxInt _index = 0.obs;
  final RxInt _tabIndex = 0.obs;
  final RxBool _isadd = false.obs;
  final RxBool _isField = true.obs;
  final RxBool idcard = true.obs;

  getidcard() {
    Get.find<HTTPS>().nogetData('/userInfo', {}).then((res) {
      print(res);
        if(res!=null){
        Get.find<HTTPS>().nogetData('/user/idcard', {}).then((res) {
                print('333333');
                print(res);
                idcard.value = res['is_ok'];
                _initConversationList();
              });
        }
       
    });
    
  }

  //  List<V2TimConversation> _data = [];

  Future<void> _initConversationList() async {
    // await ImManager().addConversationListener(
    //   onConversationUpdated: _getConversationList,
    //   onTotalUnreadMessageCountUpdated: _getTotalUnreadMessageCount,
    // );
    // await _getConversationList();
    // await _getTotalUnreadMessageCount();
  }

   //final RxList<V2TimUserFullInfo> ulist = <V2TimUserFullInfo>[].obs;

  Future<void> _getConversationList() async {
    //_data = await ImManager().getConversationList(nextSeq: '0', count: 500);

   
    // if (mounted) {
    //   setState(() {});
    // }
  }

  Future<void> _getTotalUnreadMessageCount() async {
    //  _unreadMessageCount = await ImManager().getTotalUnreadMessageCount();
  }

  Future<void> _deleteConversation(String conversationId) async {
    // final result =
    //     await ImManager().deleteConversation(conversationId: conversationId);
    // if (result) {
    //   _getConversationList();
    // } else {
    //   if (mounted) {
    //     Get.find<HTTPS>().showTost('删除失败');
    //   }
    // }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getidcard();
     
    });
  }

  @override
  void dispose() {
    //  ImManager().removeConversationListener();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final totalHeight = MediaQuery.of(context).padding.top + 15;
    // final RxBool WEB = controller.isweb;

    final RxBool WEB = Get.find<WebController>().isweb;
    if (WEB.value) {
      _index.value = 1;
    }

    return Scaffold(
      body: Stack(
        children: [
          Obx(
            () => WEB.value
                ? ListView(
                    padding: EdgeInsets.only(top: totalHeight + 95),
                    children: [WEBhome()],
                  )
                : ListView(
                    padding: EdgeInsets.only(top: totalHeight + 95),
                    children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image.asset('static/qidai.png',
                          width: 250,
                          ) 
                          ],
                        )
                      

                      // if (_data.isEmpty) isdata(),
                      // for (var item in _data.asMap().entries)
                      //   GestureDetector(
                      //     onTap: () {
                      //       // Get.toNamed('/chat/msg?userid=${item.userID}');
                      //       Get.toNamed('/chat/msg?userid=${item.value.userID}&conversationID=${item.value.conversationID}');
                      //     },
                      //     child: Slideable(
                      //       resetSlide: false,
                      //       items: [
                      //         ActionItems(
                      //           icon: Container(
                      //             alignment: Alignment.center,
                      //             color: Color(0XFFFF4343),
                      //             child: Text(
                      //               "删除",
                      //               style: TextStyle(color: Color(0XFFFFFFFF)),
                      //             ),
                      //           ),
                      //           onPress: () {
                      //             _deleteConversation(
                      //                 item.value.conversationID);
                      //           },
                      //           backgroudColor: Colors.transparent,
                      //         ),
                      //       ],
                      //       child: Stack(
                      //         children: [
                      //           Container(
                      //             padding: const EdgeInsets.symmetric(
                      //               horizontal: 15,
                      //               vertical: 15,
                      //             ),
                      //             decoration: BoxDecoration(
                      //               color: Color.fromARGB(255, 255, 255, 255),
                      //                border: Border(
                      //               bottom:
                      //                   BorderSide(color: Color(0XFFDDDDDD)))
                      //             ),


                      //             child: Row(
                      //               crossAxisAlignment:
                      //                   CrossAxisAlignment.center,
                      //               children: [
                      //                 Container(
                      //                   height: 50,
                      //                   width: 50,
                      //                   child: ClipRRect(
                      //                     borderRadius:
                      //                         BorderRadius.circular(100),
                      //                     child: Image.network(
                      //                       "${item.value.faceUrl}",
                      //                       fit: BoxFit.cover,
                      //                     ),
                      //                   ),
                      //                 ),
                      //                 SizedBox(width: 5),
                      //                 Expanded(
                      //                   child: Column(
                      //                       crossAxisAlignment:
                      //                           CrossAxisAlignment.start,
                      //                       children: [
                      //                         Text(
                      //                           "${item.value.showName}",
                      //                           style: TextStyle(
                      //                               color: Color(0xff333333),
                      //                               fontSize: 16),
                      //                         ),
                      //                         SizedBox(height: 2),
                      //                         Text(
                      //                           "${item.value.lastMessage!.textElem!.text}",
                      //                           style: TextStyle(
                      //                             color: Color(0xff999999),
                      //                           ),
                      //                         ),
                      //                       ]),
                      //                 ),
                      //                 Text(

                      //                   formatTimestamp(item.value.lastMessage!.timestamp??0),

                      //                   // formatSeconds(item.value.lastMessage!.timestamp??0),
                      //                   style: TextStyle(
                      //                     color: Color(0xff999999),
                      //                   ),
                      //                 ),
                      //               ],
                      //             ),
                      //           ),
                      //           Positioned(
                      //             left: 50,
                      //             top: 10,
                      //             child: Container(
                      //                 padding:
                      //                     EdgeInsets.symmetric(horizontal: 5),
                      //                 alignment: Alignment.center,
                      //                 decoration: BoxDecoration(
                      //                   borderRadius: BorderRadius.circular(15),
                      //                   color: Color(0XFFFF4343),
                      //                 ),
                      //                 child: item.value.unreadCount! > 0
                      //                     ? Text(
                      //                         '${item.value.unreadCount}',
                      //                         style: TextStyle(
                      //                             color: Color(0XFFFFFFFF),
                      //                             fontSize: 12),
                      //                       )
                      //                     : SizedBox()),
                      //           ),
                      //         ],
                      //       ),
                      //     ),
                      //   )
                        
                    
                    ],
                  ),
          ),
          Positioned(
            child: Container(
              alignment: Alignment.topCenter,
              color: Color(0XFFFFFFFF),
              height: 110,
              child: Image.asset(
                'static/images/hometop.png',
                fit: BoxFit.fitWidth,
                width: double.infinity,
              ),
            ),
          ),
          Obx(() => idcard.value
              ? SizedBox()
              : Align(
                  alignment: Alignment.bottomLeft, // 确保Container在底部
                  child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 15),
                      height: 45, // 高度根据需要设置
                      width: double.infinity, // 全宽覆盖底部
                      color: Color.fromARGB(
                          128, 0, 0, 0), // ARGB格式，A为透明度（0-255），这里0.5的透明度
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '需实名认证后才能购买藏品',
                            style: TextStyle(color: Color(0XFFFFFFFF)),
                          ),
                          GestureDetector(
                            onTap: () async {
                              final resul = await Get.toNamed('/certification');
                              if (resul) {
                                getidcard();
                              }
                            },
                            child: Container(
                              alignment: Alignment.center,
                              width: 83,
                              height: 28,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(25),
                                  color: Color(0xff000000)),
                              child: Text(
                                '实名认证',
                                style: TextStyle(
                                    color: Color(0XFFFFFFFF), fontSize: 14),
                              ),
                            ),
                          )
                        ],
                      )))),
          Positioned(
            child: Padding(
                padding: EdgeInsets.only(left: 15, right: 15, top: totalHeight),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        // Row(
                        //   children: [
                        //     GestureDetector(
                        //       onTap: () {
                        //         // 点击图片时的回调
                        //         print("图片被点击了！");
                        //       },
                        //       child: Image.asset(
                        //         'static/images/moves.png',
                        //         width: 18,
                        //         fit: BoxFit.fitWidth,
                        //       ),
                        //     ),
                        SizedBox(
                          width: 54,
                        ),
                        //   ],
                        // ),
                        Container(
                          decoration: BoxDecoration(
                            color: Color(0xffF1F1F1),
                            borderRadius: BorderRadius.circular(16.0),
                          ),
                          child: Padding(
                            padding: EdgeInsets.all(3),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                GestureDetector(
                                  onTap: () {
                                    _index.value = 0;
                                    Get.find<WebController>().setWeb(0);
                                  },
                                  child: Obx(() => Container(
                                      decoration: BoxDecoration(
                                        color: _index.value == 0
                                            ? Color(0xffffffff)
                                            : Color(0xfff1f1f1),
                                        borderRadius:
                                            BorderRadius.circular(16.0),
                                      ),
                                      width: 80,
                                      height: 26,
                                      child: Stack(
                                        children: [
                                          Align(
                                            alignment: Alignment.center,
                                            child: Text('OSG',
                                                style: TextStyle(fontSize: 16)),
                                          ),
                                          Positioned(
                                            // 第一个层叠的小部件
                                            right: 18,
                                            top: 2,
                                            child: Container(
                                              decoration: BoxDecoration(
                                                  color: Color(0xffFF4343),
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          10)),
                                              width: 6,
                                              height: 6,
                                            ),
                                          ),
                                        ],
                                      ))),
                                ),
                                GestureDetector(
                                  onTap: () {
                                    // 点击事件处理
                                    _index.value = 1;
                                    Get.find<WebController>().setWeb(1);
                                  },
                                  child: Obx(() => Container(
                                        decoration: BoxDecoration(
                                          color: _index.value == 1
                                              ? Color(0xffffffff)
                                              : Color(0xfff1f1f1),
                                          borderRadius:
                                              BorderRadius.circular(16.0),
                                        ),
                                        width: 80,
                                        height: 26,
                                        child: Align(
                                          alignment: Alignment.center,
                                          child: Text('Web3',
                                              style: TextStyle(fontSize: 16)),
                                        ),
                                      )),
                                ),
                              ],
                            ),
                          ),
                        ),
                        Row(
                          children: [
                            Obx(() => WEB.value
                                ? GestureDetector(
                                    onTap: () {
                                      // 点击图片时的回调
                                      Get.toNamed(
                                          '/web_tabs/announcement/index');
                                    },
                                    child: Image.asset(
                                      'static/images/xingonggao.png',
                                      width: 18,
                                      fit: BoxFit.fitWidth,
                                    ),
                                  )
                                : GestureDetector(
                                    onTap: () {
                                      // 点击图片时的回调
                                      print("图片被点击了111！");
                                      _isadd.value = !_isadd.value;
                                    },
                                    child: Image.asset(
                                      'static/add.png',
                                      width: 18,
                                      fit: BoxFit.fitWidth,
                                    ),
                                  )),
                            SizedBox(width: 18),
                            GestureDetector(
                              onTap: () {
                                // 点击图片时的回调
                                print("图片被点击了！");
                                Get.toNamed('/invitation');
                              },
                              child: Image.asset(
                                'static/images/xiangzi.png',
                                width: 18,
                                fit: BoxFit.fitWidth,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                    Obx(
                      () => WEB.value
                          ? Container(
                              margin: EdgeInsets.only(top: 15),
                              padding: EdgeInsets.symmetric(
                                  vertical: 15, horizontal: 12),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  color: Color(0XFFF8F9FD)),
                              child: Row(
                                children: [
                                  Image.asset(
                                    'static/web/gonggao.png',
                                    width: 28,
                                    fit: BoxFit.fitWidth,
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Container(
                                    width: 1,
                                    height: 20,
                                    color: Color(0XFFD8D8D8),
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Expanded(
                                    child: MyCarousel(),
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  GestureDetector(
                                    onTap: () => {
                                      Get.toNamed(
                                          '/web_tabs/announcement/index')
                                    },
                                    child: Image.asset(
                                      'static/web/shangang.png',
                                      width: 16,
                                      fit: BoxFit.fitWidth,
                                    ),
                                  )
                                ],
                              ),
                            )
                          : Container(
                              margin:
                                  EdgeInsets.only(left: 15, right: 15, top: 15),
                              height: 40,
                              child: Stack(
                                alignment: Alignment.center,
                                children: [
                                  TextField(
                                    onChanged: (value) {
                                      print("文本已改变: $value");
                                      if (value.isNotEmpty) {
                                        _isField.value = false;
                                      } else {
                                        _isField.value = true;
                                      }
                                    },
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                      color: Colors.black, // 文本颜色
                                      fontSize: 12.0, // 字体大小
                                    ),
                                    decoration: InputDecoration(
                                      focusedBorder: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: Colors.white,
                                            width: 0), // 激活状态下的边框颜色和宽度
                                        borderRadius: BorderRadius.circular(
                                            10.0), // 设置圆角边框半径
                                      ),
                                      enabledBorder: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: Colors.white,
                                            width: 0), // 激活状态下的边框颜色和宽度
                                        borderRadius: BorderRadius.circular(
                                            10.0), // 设置圆角边框半径
                                      ),
                                      filled: true,
                                      fillColor: Color(0xfff5f5f5),
                                      hintText: '搜索',
                                      hintStyle: TextStyle(
                                          fontSize: 14,
                                          color: Color(0xff999999)),
                                      border: InputBorder.none,
                                    ),
                                  ),
                                  Obx(() => _isField.value
                                      ? Positioned(
                                          // 第一个层叠的小部件
                                          left: 0,
                                          right: 40,
                                          top:12,
                                          child: Icon(Icons.search,
                                              size: 15,
                                              color: Color(0xff999999)),
                                        )
                                      : SizedBox()),
                                ],
                              ),
                            ),
                    )
                  ],
                )),
          ),
          Obx(() => _isadd.value
              ? Positioned(
                  right: 10,
                  top: totalHeight + 45,
                  child: Container(
                      decoration: BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                              color: Color.fromRGBO(0, 0, 0, 0.1608),
                              blurRadius: 20, // 阴影模糊程度
                              spreadRadius: 1 // 阴影扩散程度
                              ),
                        ],
                        color: Color.fromARGB(255, 255, 255, 255),
                        borderRadius: BorderRadius.circular(5.0),
                      ),
                      width: 120,
                      height: 90,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          GestureDetector(
                            onTap: () {
                              _tabIndex.value = 0;
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('static/images/addqun.png',
                                    width: 16, fit: BoxFit.fitWidth),
                                SizedBox(width: 5),
                                Text('加入群聊', style: TextStyle(fontSize: 16))
                              ],
                            ),
                          ),
                          Divider(height: 30,color: Color(0XFFDDDDDD),),
                          GestureDetector(
                            onTap: () {
                              _tabIndex.value = 1;
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('static/images/cerqun.png',
                                    width: 16, fit: BoxFit.fitWidth),
                                SizedBox(width: 5),
                                Text('创建群聊', style: TextStyle(fontSize: 16))
                              ],
                            ),
                          ),
                        ],
                      )),
                )
              : SizedBox()),
          Obx(() => _isadd.value
              ? Positioned(
                  right: 50,
                  top: totalHeight + 31,
                  child: Image.asset('static/top.png',
                      width: 16, fit: BoxFit.fitWidth))
              : SizedBox()),
        ],
      ),
    );
  }
}



// import 'package:flutter/material.dart';
// import 'package:get/get.dart';

// class HomePage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//            appBar: AppBar(
//             backgroundColor: Color(0xffffffff),
//             title: Text('平台公告'),
//             centerTitle: true
//           ),
//     );
//   }
// }


 